#!/bin/bash
HOME_PATH=${HOME}
SAVE_PATH=${HOME}/Source

gmp_compressed=gmp-6.2.0.tar.xz
mpdr_compressed=mpfr-4.1.0.zip
mpc_compressed=mpc-1.2.0.tar.gz
isl_compressed=isl-0.18.tar.bz2
gcc_compressed=gcc-7.5.0.tar.xz

gcc_path=`echo ${gcc_compressed}|grep -Eo "\<[a-z].+[0-9]"`
echo "You will download file $gmp_compressed"
echo "You will download file $gmp_compressed"
echo "You will download file $mpdr_compressed"
echo "You will download file $isl_compressed"
echo "You will download file $gcc_compressed"
echo "You choose gcc version:${gcc_path}"
function dep_zip(){
    SAVE_PATH=${1}
    if [ ! -d ${SAVE_PATH} ];then
        mkdir ${SAVE_PATH}
    fi

	if [ ! -f ${SAVE_PATH}/${gmp_compressed} ];then
        wget -c https://mirrors.tuna.tsinghua.edu.cn/gnu/gmp/${gmp_compressed} -P ${SAVE_PATH}
	else
		echo "File ${gmp_compressed} had been download!"
	fi
	if [ ! -f ${SAVE_PATH}/${mpdr_compressed} ];then
        wget -c https://mirrors.tuna.tsinghua.edu.cn/gnu/mpfr/${mpdr_compressed} -P ${SAVE_PATH}
	else
		echo "File ${mpdr_compressed} had been download!"
	fi

	if [ ! -f ${SAVE_PATH}/${mpc_compressed} ];then
        wget -c https://mirrors.tuna.tsinghua.edu.cn/gnu/mpc/${mpc_compressed} -P ${SAVE_PATH}
	else
		echo "File ${mpc_compressed} had been download!"
	fi
	if [ ! -f ${SAVE_PATH}/${isl_compressed} ];then
        wget -c ftp://gcc.gnu.org/pub/gcc/infrastructure/${isl_compressed} -P ${SAVE_PATH}
	else
		echo "File ${isl_compressed} had been download!"
	fi
	if [ ! -f ${SAVE_PATH}/${gmp_compressed} ];then
        wget -c https://mirrors.tuna.tsinghua.edu.cn/gnu/gcc/${gcc_path}/${gcc_compressed} -P ${SAVE_PATH}
	else
		echo "File ${gmp_compressed} had been download!"
	fi
}
echo "Download need software into ${SAVE_PATH}!"
dep_zip ${SAVE_PATH}

default_install_path=/usr/local/${gcc_path}
install_path=${default_install_path}
read -p "Please input install path,default is:(${default_install_path}) [Y/y]: " choose
if [ "${choose}" == "y" -o "${choose}" == "Y" -o "${choose}" == "" ];then
    echo "You Choose install path:${install_path}"
else
    read -p "Please input your install path: " install_path
    echo "Your input install path is:${install_path}"
fi

echo "==> begin to install gmp!"
echo "file ${SAVE_PATH}/${gmp_compressed}"
tar -xJvf ${SAVE_PATH}/${gmp_compressed} -C /tmp
current_path=`echo ${gmp_compressed}|grep -Eo "\<[a-z].+[0-9]"`
cd /tmp/${current_path}
echo "Current Path: /tmp/${current_path}"
./configure --prefix=${install_path}
make -j8
sudo make install
echo "==> gmp module installed!"

echo "==> begin to install mpfr-4.1.0!"
tar -xJvf ${SAVE_PATH}/${mpdr_compressed} -C /tmp
current_path=`echo ${mpdr_compressed}|grep -Eo "\<[a-z].+[0-9]"`
cd /tmp/${current_path}
./configure --prefix=${install_path} --with-gmp-include=${install_path}/include/ -- with-gmp-lib=${install_path}/lib/
make -j8
sudo make install
echo "==> mpfr-4.1.0 module installed!"

echo "==> begin to install ${mpc_compressed}!"
tar -xJvf ${SAVE_PATH}/${mpc_compressed} -C /tmp
current_path=`echo ${mpc_compressed}|grep -Eo "\<[a-z].+[0-9]"`
cd /tmp/${current_path}
./configure --prefix=${install_path} --with-gmp-include=${install_path}/include/ -- with-gmp-lib=${install_path}/lib/
make -j8
sudo make install
echo "==> ${mpc_compressed} install finished!"

echo "==> ${isl_compressed} module begin to install!"
tar -xJvf ${SAVE_PATH}/${isl_compressed} -C /tmp
current_path=`echo ${isl_compressed}|grep -Eo "\<[a-z].+[0-9]"`
cd /tmp/${current_path}
./configure --prefix=${install_path} --with-gmp-prefix=${install_path}
make -j8
sudo make install
echo "==> isl-0.18 module installed!"

echo "==> ${gcc_compressed} begin to install!"
tar -xJvf ${SAVE_PATH}/${gcc_compressed} -C /tmp
current_path=`echo ${isl_compressed}|grep -Eo "\<[a-z].+[0-9]"`
cd /tmp/${current_path}

./configure --prefix=${install_path} --with-gmp=${install_path} --with-gmp-lib=${install_path}/lib --with-gmp-include=${install_path}/include --with-isl-lib=${install_path}/lib/ --with-isl-include=${install_path}/include  --disable-multilib
make -j8
sudo make install
echo "==> ${gcc_compressed} module installed!"
echo "==> Add env variable"

zsh_env=${HOME}/.zshrc
bash_env=${HOME}/.bashrc
if [ -f ${zsh_env} ];then
	check_var=`grep ${gcc_path} ${zsh_env}`
	if [ "${check_var}" == "" ];then
        sed -i "\$a PATH=\${PATH}:${install_path}/bin" ${zsh_env}
	fi
    source ${zsh_env}
else
	check_var=`grep ${gcc_path} ${bash_env}`
	if [ "${check_var}" == "" ];then
	    sed -i "\$a PATH=\${PATH}:${install_path}/bin" ${bash_env}
	fi
	source ${bash_env}
fi

